package com.grantheath.erebus.controller.transitionManagers.linearTransitionManagers.normalFlow 
{
	import com.grantheath.erebus.model.enum.notifications.ErebusPreloaderNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusTransitionNotifications;
	import com.grantheath.erebus.model.enum.notifications.ErebusPageNotifications;
	import com.grantheath.erebus.model.interfaces.IErebusSiteTreeProxy;
	import com.grantheath.erebus.model.interfaces.IErebusPageNode;
	import com.grantheath.erebus.model.enum.names.ErebusProxyNames;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	/**
	 * @author grantheath
	 */
	public class ErebusPageUninitializedNormalFlowCommand extends SimpleCommand implements ICommand 
	{
		override public function execute(notification : INotification) : void
		{
			var siteTreeProxy : IErebusSiteTreeProxy = facade.retrieveProxy(ErebusProxyNames.SITE_TREE_PROXY) as IErebusSiteTreeProxy;
			
			if(siteTreeProxy.interuptTransition){
				sendNotification(ErebusTransitionNotifications.TRANSITION_INTERUPTED);
			} else {
				var pageNode : IErebusPageNode = siteTreeProxy.incrementHideQueue();
				
				siteTreeProxy.incrementUninitializeQueue();
				if(pageNode){
					sendNotification(ErebusPageNotifications.HIDE_PAGE, pageNode);
				} else {
					pageNode = siteTreeProxy.displayQueue[0] as IErebusPageNode;
					sendNotification(ErebusTransitionNotifications.PAGE_UNINITIALIZE_PROCESS_COMPLETE);
					if(pageNode) {
						sendNotification(ErebusPreloaderNotifications.SHOW_PRELOADER, pageNode);
					} else {
						sendNotification(ErebusTransitionNotifications.PAGE_UNINITIALIZE_PROCESS_COMPLETE);
						sendNotification(ErebusTransitionNotifications.TRANSITION_COMPLETE);
					}
				}
			}
		}
	}
}
